//
//  DeviceMoreInfoViewController.m
//  sentorise
//
//  Created by admin on 2025/8/28.
//

#import "DeviceMoreInfoViewController.h"
#import "GlobalDefine.h"
#import "BTManager.h"
#import "ProgressHUD.h"

@interface DeviceMoreInfoViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *snLabel;
@property (weak, nonatomic) IBOutlet UILabel *cycleLabel;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *itemTitleLabel1;
@property (weak, nonatomic) IBOutlet UILabel *itemTitleLabel2;
@property (weak, nonatomic) IBOutlet UILabel *itemTitleLabel3;
@property (weak, nonatomic) IBOutlet UILabel *itemTitleLabel4;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation DeviceMoreInfoViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topSpace.constant = IphoneX ? 54 : 20;
    self.nameLabel.text = [[BTManager sharedBTManager].diviceNameDict objectForKey:self.model.name] ?: self.model.name;
    self.cycleLabel.text = self.cycles;

    self.titleLabel.text = NSLocalizedString(@"More Info", nil);
    self.itemTitleLabel1.text = NSLocalizedString(@"Device Name", nil);
    self.itemTitleLabel2.text = NSLocalizedString(@"SN Code", nil);
    self.itemTitleLabel3.text = NSLocalizedString(@"Number of cycles", nil);
    self.itemTitleLabel4.text = NSLocalizedString(@"Firmware version", nil);

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReciveData:) name:kBTDidReciveData object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchDevicesName:) name:kBTSearchDevicesName object:nil];

    [self startQueruy];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

- (void)startQueruy {
    WeakSelf
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (![BTManager sharedBTManager].peripheral) {
            return;
        }
        [weakSelf queryFirmware];
    }];
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionModifyName:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Modify bluetooth name", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Confirm", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *nameIntervalTextfield = alertController.textFields.firstObject;
        NSString *nameInterval = nameIntervalTextfield.text;
        if (nameInterval.length) {
            if (nameInterval.length > 16) {
                [ProgressHUD showErrorMessage:@"Bluetooth name cannot exceed 16 characters"];
                return;
            }
            nameInterval = [NSString stringWithFormat:@"AT+NAME=BT_%@,0", nameInterval];
            [[BTManager sharedBTManager] sendCommandModifyName:nameInterval];
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = NSLocalizedString(@"Please enter the bluetooth name(16 characters)", nil);
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (IBAction)actionCopy:(id)sender {
    [UIPasteboard generalPasteboard].string = self.snLabel.text;
    [ProgressHUD showSuccessMessage:@"Copy successful"];
}

- (void)searchDevicesName:(NSNotification *)nf {
    self.nameLabel.text = [[BTManager sharedBTManager].diviceNameDict objectForKey:self.model.name] ?: self.model.name;
}

- (void)queryFirmware {
    Byte bytes[8];
    bytes[0] = 0x01;
    bytes[1] = 0x03;
    bytes[2] = 0xC0;
    bytes[3] = 0x02;
    bytes[4] = 0x00;
    bytes[5] = 0x30;
    bytes[6] = 0x00;
    bytes[7] = 0x00;
    [[BTManager sharedBTManager] sendCommandWithCrc:bytes length:8];
}

- (void)didReciveData:(NSNotification *)notification {
    NSData *reciveData = notification.object;
    Byte *dataBytes = (Byte*)reciveData.bytes;
    if(dataBytes[2] == 0x60) {
        NSString *sn = @"";
        for (NSInteger i=0; i<32; i++) {
            char c = (char)dataBytes[i + 3];
            sn = [NSString stringWithFormat:@"%@%c", sn, c];
        }
        self.snLabel.text = sn;
        NSString *version = @"";
        for (NSInteger i=0; i<32; i++) {
            char c = (char)dataBytes[i + 3 + 32];
            version = [NSString stringWithFormat:@"%@%c", version, c];
        }
        self.versionLabel.text = version;
    }
}


@end
